package com.dandelion.manager.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dandelion.commons.spring.SpringContextHolder;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.commons.web.model.DataGrid;
import com.dandelion.manager.system.domain.Auth;
import com.dandelion.manager.system.repository.AuthMapper;
import com.dandelion.manager.system.service.AuthService;

@Service
public class AuthServiceImpl implements AuthService {
	
	@Autowired
	private AuthMapper mapper ;

	@Override
	public AjaxResponse add(Auth record) {
		this.mapper.insertSelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}
	
	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}


	@Override
	public AjaxResponse update(Auth record) {
		this.mapper.updateByPrimaryKeySelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public Auth get(long id) {
		return this.mapper.selectByPrimaryKey(id) ;
	}

	@Override
	public List<Auth> getAllList(Auth record) {
		return this.mapper.selectAllList(record);
	}
	
	@Override
	public DataGrid<Auth> datagrid(Auth conditions) {
		DataGrid<Auth> dg = new DataGrid<Auth>();

		Map<String, Object> conditionsMap = new HashMap<String, Object>();
		conditionsMap.put("record", conditions);
		conditionsMap.put("offset", dg.getOffset());
		conditionsMap.put("rows", dg.getPageSize());

		List<Auth> list = this.mapper.selectAllListPage(conditionsMap);
		int count = this.mapper.getCount(conditionsMap);
		dg.setRows(list);
		dg.setTotal(count);

		return dg;
	}

	@Override
	public void deleteAuthByUserId(Long[] userIds) {
		this.mapper.deleteAuthByUserId(userIds);
	}

	@Override
	public Auth getAuthByKey(Auth record) {
		return this.mapper.getAuthByKey(record);
	}
	
}
